PROJECT = ~/mas/project
EX_IDM    = ../../../test/ex_idm.sh
PROFILER  = python -m cProfile -s time
#PROFILER  = python -m memory_profiler
#PROFILER  = time python
LEARN = ./learn.py
CLASSIFY = ./classify.py
PLOT = ./plot.py

# Correctness test params
C_DIM        = 10
C_TS_SIZE    = 10
C_TDATA      = $(PROJECT)/data/sparse_idm/id10_1000.dat
C_SIZE_X     = 100
C_SIZE_Y     = 100
C_SOM_INI    = c_som_ini.dat
C_SOM_FIN    = c_som_fin.dat
C_CLASS_DATA = c_class.dat
# avoid showing progress per iter chunk, epochs are frequent enough
C_SHOW_PROG  = 100000
C_LEARN_OUT  = c_learn.out
C_CLASS_OUT  = c_class.out
C_MIN_W      = 0
C_MAX_W      = 0.1
C_EPOCHS     = 2
C_DEBUG      = 0

# Grimm tales (semantic maps)
G_PRECLASS   = $(PROJECT)/data/semap2/vocab.txt
G_COLORMAP   = g_color.map
G_TDATA      = $(PROJECT)/data/semap2/encdata.txt
G_DIM        = $(shell cat  $(G_PRECLASS) | wc -l)
#G_TS_SIZE    = $(shell cat $(G_TDATA) | wc -l)
G_TS_SIZE    = 100
G_SIZE_X     = $(shell echo "sqrt(10*$(G_DIM))+1; scale=2" | bc)
G_SIZE_Y     = $(G_SIZE_X)
G_SOM_INI    = g_som_ini.dat
G_SOM_FIN    = g_som_fin.dat
G_CLASS_DATA = g_class.dat
G_SHOW_PROG  = 1
G_LEARN_OUT  = g_learn.out
G_CLASS_OUT  = g_class.out
G_MIN_W      = 0
G_MAX_W      = 0.1
G_EPOCHS     = 1
G_DEBUG      = 0
G_PLOT_OUT   = g_plot.png

all: learn_g class_g plot_g

learn_c: $(LEARN)
	$(PROFILER) $(LEARN) $(C_DIM) $(C_TS_SIZE) $(C_TDATA) $(C_SIZE_X) $(C_SIZE_Y) $(C_MIN_W) $(C_MAX_W) $(C_SOM_INI) $(C_SOM_FIN) $(C_EPOCHS) $(C_DEBUG) $(C_SHOW_PROG) > $(C_LEARN_OUT) 2>&1
		$(EX_IDM) $(C_SOM_FIN) >> $(C_LEARN_OUT)
	tail -$(C_DIM) $(C_LEARN_OUT)

class_c: $(CLASSIFY)
	$(PROFILER) $(CLASSIFY) $(C_DIM) $(C_TS_SIZE) $(C_TDATA) $(C_SIZE_X) $(C_SIZE_Y) $(C_SOM_FIN) $(C_CLASS_DATA) $(C_DEBUG) $(C_SHOW_PROG) > $(C_CLASS_OUT) 2>&1

learn_g: $(LEARN)
	@echo vocab size = $(G_DIM), trainset size = $(G_TS_SIZE), \
        som dim = [$(G_SIZE_X),$(G_SIZE_Y)] > $(G_LEARN_OUT)
	@tail -1 $(G_LEARN_OUT)
	$(PROFILER) $(LEARN) $(G_DIM) $(G_TS_SIZE) $(G_TDATA) $(G_SIZE_X) $(G_SIZE_Y) $(G_MIN_W) $(G_MAX_W) $(G_SOM_INI) $(G_SOM_FIN) $(G_EPOCHS) $(G_DEBUG) $(G_SHOW_PROG) > $(G_LEARN_OUT) 2>&1

class_g: $(CLASSIFY)
	$(PROFILER) $(CLASSIFY) $(G_DIM) $(G_TS_SIZE) $(G_TDATA) $(G_SIZE_X) $(G_SIZE_Y) $(G_SOM_FIN) $(G_CLASS_DATA) $(G_DEBUG) $(G_SHOW_PROG) > $(G_CLASS_OUT) 2>&1

plot_g: $(PLOT)
	$(PROFILER) $(PLOT) $(G_CLASS_DATA) $(G_PRECLASS) $(G_COLORMAP) $(G_PLOT_OUT)
	display $(G_PLOT_OUT)

clean:
	rm -fr nohup.out *.pyc ./build *.so *.c __pycache__
	rm -f $(C_SOM_INI) $(C_SOM_FIN) $(C_LEARN_OUT) $(C_CLASS_OUT) $(C_CLASS_DATA)
	rm -f $(G_SOM_INI) $(G_SOM_FIN) $(G_LEARN_OUT) $(G_CLASS_OUT) $(G_CLASS_DATA) $(G_PLOT_OUT)
